import { customRef } from 'vue';

export function useStoreRef<T>(key: string, value?: T) {
  return customRef<T>((track, trigger) => {
    return {
      get() {
        track();
        if (value) {
          return value;
        } else {
          let temp = sessionStorage.getItem(key);
          if (temp) {
            return JSON.parse(temp);
          }
        }
        return value;
      },
      set(newValue) {
        value = newValue;
        console.log(value);

        sessionStorage.setItem(key, JSON.stringify(newValue));
        trigger();
      },
    };
  });
}
